package yxy.game.pm2.command.dungeon;

import org.joda.time.DateTime;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_冲榜;
import yxy.game.pm2.bean.module.rank.active.RankRushEnum;
import yxy.game.pm2.bean.module.rank.active.RankRushHandle;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.DungeonMessage;
import yxy.game.pm2.module.dungeon.DungeonHandle;
import yxy.game.pm2.module.dungeon.DungeonModule;
import yxy.game.pm2.module.dungeon.DungeonType;

/** 日常副本 一键扫荡 */
@MessageType(proto = Command.DungeonSweepOneButton, classtype = DungeonMessage.DungeonSweepOneButtonRequest.class)
public class CMD_DungeonSweepOneButton extends Command<DungeonMessage.DungeonSweepOneButtonRequest> {

	@Override
	public void execute(MessageSource messageSource, DungeonMessage.DungeonSweepOneButtonRequest request) {
		String playerCode = messageSource.getTag();
		int type = request.getType();

		DungeonMessage.DungeonSweepOneButtonResponse.Builder response = DungeonMessage.DungeonSweepOneButtonResponse.newBuilder();

		try {
			Result result = new DungeonHandle().sweepOneButton(playerCode, type, response);

			//todo 每次循环的前半小时内不开启榜
			if (result.equals(Result.Success)) {
				ActiveModule_冲榜 am = ActiveModule_冲榜.get(playerCode, false);
				DungeonModule dungeonModule = DungeonModule.get(playerCode, DungeonType.forNumber(type));
				int freetimes = dungeonModule.freeTimes();
				int paytimes = dungeonModule.payTimes();
				int times = paytimes + freetimes;
				DateTime begin = am.getRunend().minusDays(6).withHourOfDay(0).withMinuteOfHour(30).withSecondOfMinute(0).withMillisOfSecond(0);
				if (!DateTime.now().isBefore(begin)) {
					RankRushHandle.addRankRushData(playerCode, times, RankRushEnum.冲榜_副本关卡.getType());
				}
			}

			send(messageSource, response, result);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, DungeonMessage.DungeonSweepOneButtonResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}